{
  "name": "Case 41: Loan Covenant Monitoring - FINAL PRODUCTION VERSION",
  "nodes": [
    {
      "parameters": {
        "pollTimes": {
          "item": [
            {
              "mode": "everyMinute"
            }
          ]
        },
        "triggerOn": "specificFolder",
        "folderToWatch": {
          "__rl": true,
          "value": "1KVYvmrXNW48iR_qTA438K4IsF4TbRb8y",
          "mode": "list",
          "cachedResultName": "Loan Covenant Monitoring",
          "cachedResultUrl": "https://drive.google.com/drive/folders/1KVYvmrXNW48iR_qTA438K4IsF4TbRb8y"
        },
        "event": "fileCreated",
        "options": {}
      },
      "id": "eb3db613-db7a-4167-8212-d174abfc5bcb",
      "name": "Quarterly Report Trigger",
      "type": "n8n-nodes-base.googleDriveTrigger",
      "typeVersion": 1,
      "position": [
        -1840,
        -144
      ],
      "credentials": {
        "googleDriveOAuth2Api": {
          "id": "Cf6MNES8JrQntp3r",
          "name": "Google Drive account"
        }
      }
    },
    {
      "parameters": {
        "operation": "download",
        "fileId": {
          "__rl": true,
          "value": "={{ $json.id }}",
          "mode": "id"
        },
        "options": {}
      },
      "id": "5ae30423-56cb-457f-bd41-94ff5d5c229c",
      "name": "Download PDF",
      "type": "n8n-nodes-base.googleDrive",
      "typeVersion": 3,
      "position": [
        -1648,
        -144
      ],
      "credentials": {
        "googleDriveOAuth2Api": {
          "id": "Cf6MNES8JrQntp3r",
          "name": "Google Drive account"
        }
      }
    },
    {
      "parameters": {
        "method": "POST",
        "url": "https://api.cloud.llamaindex.ai/api/v1/parsing/upload",
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "Authorization",
              "value": "Bearer llx-7BWYrvPwfUhgdXbZMtaNCsr2O0h7JH4dkHjHisxcvq46bkIj"
            }
          ]
        },
        "sendBody": true,
        "contentType": "multipart-form-data",
        "bodyParameters": {
          "parameters": [
            {
              "parameterType": "formBinaryData",
              "name": "file",
              "inputDataFieldName": "data"
            }
          ]
        },
        "options": {}
      },
      "id": "a2c1eda5-57b4-41c5-b870-0fd6d74dd15a",
      "name": "LlamaIndex POST",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.3,
      "position": [
        -1440,
        -144
      ]
    },
    {
      "parameters": {
        "amount": 20
      },
      "id": "865911e3-238f-474d-aa51-14ae74f29358",
      "name": "Wait",
      "type": "n8n-nodes-base.wait",
      "typeVersion": 1.1,
      "position": [
        -1248,
        -144
      ],
      "webhookId": "30cc7ddd-1dd7-4186-ae6a-00e0d2161d17"
    },
    {
      "parameters": {
        "url": "=https://api.cloud.llamaindex.ai/api/v1/parsing/job/{{ $node[\"LlamaIndex POST\"].json[\"id\"] }}",
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "Authorization",
              "value": "Bearer llx-7BWYrvPwfUhgdXbZMtaNCsr2O0h7JH4dkHjHisxcvq46bkIj"
            }
          ]
        },
        "options": {}
      },
      "id": "1742e287-23c7-4104-935b-1d56859123e2",
      "name": "Check Status",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.3,
      "position": [
        -1040,
        -144
      ]
    },
    {
      "parameters": {
        "rules": {
          "values": [
            {
              "conditions": {
                "options": {
                  "caseSensitive": true,
                  "leftValue": "",
                  "typeValidation": "strict",
                  "version": 2
                },
                "conditions": [
                  {
                    "leftValue": "={{ $json.status }}",
                    "rightValue": "SUCCESS",
                    "operator": {
                      "type": "string",
                      "operation": "equals"
                    },
                    "id": "be95d49a-e42d-49f9-9fbc-7105164642ce"
                  }
                ],
                "combinator": "and"
              },
              "renameOutput": true,
              "outputKey": "SUCCESS"
            },
            {
              "conditions": {
                "options": {
                  "caseSensitive": true,
                  "leftValue": "",
                  "typeValidation": "strict",
                  "version": 2
                },
                "conditions": [
                  {
                    "id": "ab5ca84a-5c0c-4e88-850a-e631b688a8d1",
                    "leftValue": "={{ $json.status }}",
                    "rightValue": "PENDING",
                    "operator": {
                      "type": "string",
                      "operation": "equals",
                      "name": "filter.operator.equals"
                    }
                  }
                ],
                "combinator": "and"
              },
              "renameOutput": true,
              "outputKey": "PENDING"
            }
          ]
        },
        "options": {}
      },
      "id": "ac66d1e2-ea11-408d-a5c0-b024c8308b5d",
      "name": "Status Switch",
      "type": "n8n-nodes-base.switch",
      "typeVersion": 3.3,
      "position": [
        -848,
        -144
      ]
    },
    {
      "parameters": {
        "url": "=https://api.cloud.llamaindex.ai/api/v1/parsing/job/{{ $node[\"LlamaIndex POST\"].json[\"id\"] }}/result/markdown",
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "Authorization",
              "value": "Bearer llx-7BWYrvPwfUhgdXbZMtaNCsr2O0h7JH4dkHjHisxcvq46bkIj"
            }
          ]
        },
        "options": {}
      },
      "id": "e697235a-ee92-431a-804e-250b3f3131d1",
      "name": "Get Markdown",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.3,
      "position": [
        -640,
        -240
      ]
    },
    {
      "parameters": {
        "text": "={{ $json.markdown }}",
        "attributes": {
          "attributes": [
            {
              "name": "Borrower Name",
              "description": "The legal name of the entity that borrowed the funds."
            },
            {
              "name": "Loan ID",
              "description": "The unique alphanumeric identifier for the loan facility."
            },
            {
              "name": "Facility Type",
              "description": "Extract the loan type mentioned, specifically look for phrases like 'CAPEX repayment schedule' or 'Credit Agreement'. If found, return 'CAPEX Facility'."
            },
            {
              "name": "Total Debt (USD)",
              "description": "The current total outstanding debt balance in USD."
            },
            {
              "name": "EBITDA (USD)",
              "description": "Earnings Before Interest, Taxes, Depreciation, and Amortization."
            },
            {
              "name": "Interest Coverage Ratio",
              "description": "Look for 'Interest Coverage' or calculate it by dividing EBITDA ($2,100,000) by the annual interest expense if stated. If not stated, return 'Calculated: N/A - Missing Interest Expense'."
            },
            {
              "name": "Leverage Limit",
              "description": "The maximum Debt/EBITDA ratio allowed by the covenant."
            },
            {
              "name": "Actual Leverage",
              "description": "The borrower's current Debt/EBITDA ratio calculation."
            },
            {
              "name": "Liquidity Threshold (USD)",
              "description": "The minimum cash or equivalents required by the covenant."
            },
            {
              "name": "Actual Liquidity (USD)",
              "description": "The borrower's current available liquidity in USD."
            },
            {
              "name": "Compliance Status",
              "description": "Categorize as 'Compliant', 'Breach', or 'Watchlist'."
            },
            {
              "name": "Review Date",
              "description": "The date the financial data was reported or reviewed."
            },
            {
              "name": "Risk Level",
              "description": "Assign 'Critical' if the status for both Leverage and Liquidity is 'NON-COMPLIANT'. Otherwise, assign 'High' if only one is breached."
            }
          ]
        },
        "options": {
          "systemPromptTemplate": "You are a financial auditor. Extract the exact loan covenant metrics from the provided text. Return 'N/A' for any missing values."
        }
      },
      "id": "a0f05bc7-31ca-4a7d-8763-d2fbe2026a03",
      "name": "Risk Intelligence Extractor",
      "type": "@n8n/n8n-nodes-langchain.informationExtractor",
      "typeVersion": 1.2,
      "position": [
        -448,
        -240
      ]
    },
    {
      "parameters": {
        "jsCode": "return [{ json: $node[\"Risk Intelligence Extractor\"].json.output }];"
      },
      "id": "a1926990-1c3f-4e4b-a252-a7415faf4408",
      "name": "Split Risk Rows",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        -160,
        -240
      ]
    },
    {
      "parameters": {
        "operation": "append",
        "documentId": {
          "__rl": true,
          "value": "1le3bFkRD_eURqCk6e9QBZovZIGEMmFAU7SA2Q_Q0ZVU",
          "mode": "list",
          "cachedResultName": "Case_41_Loan_Covenant_Monitoring_Master",
          "cachedResultUrl": "https://docs.google.com/spreadsheets/d/1le3bFkRD_eURqCk6e9QBZovZIGEMmFAU7SA2Q_Q0ZVU/edit?usp=drivesdk"
        },
        "sheetName": {
          "__rl": true,
          "value": "gid=0",
          "mode": "list",
          "cachedResultName": "Sheet1",
          "cachedResultUrl": "https://docs.google.com/spreadsheets/d/1le3bFkRD_eURqCk6e9QBZovZIGEMmFAU7SA2Q_Q0ZVU/edit#gid=0"
        },
        "columns": {
          "mappingMode": "autoMapInputData",
          "value": {},
          "matchingColumns": [],
          "schema": [
            {
              "id": "Borrower Name",
              "displayName": "Borrower Name",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true,
              "removed": false
            },
            {
              "id": "Loan ID",
              "displayName": "Loan ID",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true,
              "removed": false
            },
            {
              "id": "Facility Type",
              "displayName": "Facility Type",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true,
              "removed": false
            },
            {
              "id": "Total Debt (USD)",
              "displayName": "Total Debt (USD)",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true,
              "removed": false
            },
            {
              "id": "EBITDA (USD)",
              "displayName": "EBITDA (USD)",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true,
              "removed": false
            },
            {
              "id": "Interest Coverage Ratio",
              "displayName": "Interest Coverage Ratio",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true,
              "removed": false
            },
            {
              "id": "Leverage Limit",
              "displayName": "Leverage Limit",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true,
              "removed": false
            },
            {
              "id": "Actual Leverage",
              "displayName": "Actual Leverage",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true,
              "removed": false
            },
            {
              "id": "Liquidity Threshold (USD)",
              "displayName": "Liquidity Threshold (USD)",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true,
              "removed": false
            },
            {
              "id": "Actual Liquidity (USD)",
              "displayName": "Actual Liquidity (USD)",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true,
              "removed": false
            },
            {
              "id": "Compliance Status",
              "displayName": "Compliance Status",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true,
              "removed": false
            },
            {
              "id": "Review Date",
              "displayName": "Review Date",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true,
              "removed": false
            },
            {
              "id": "Risk Level",
              "displayName": "Risk Level",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true,
              "removed": false
            }
          ],
          "attemptToConvertTypes": false,
          "convertFieldsToString": false
        },
        "options": {}
      },
      "id": "d9b94063-f124-43d2-aee0-c5adb09af686",
      "name": "Log Risk Rows",
      "type": "n8n-nodes-base.googleSheets",
      "typeVersion": 4.7,
      "position": [
        16,
        -240
      ],
      "credentials": {
        "googleSheetsOAuth2Api": {
          "id": "LOs2dbk9lby0NfDM",
          "name": "Google Sheets account"
        }
      }
    },
    {
      "parameters": {
        "model": {
          "__rl": true,
          "value": "gpt-4o-mini",
          "mode": "list"
        },
        "builtInTools": {},
        "options": {}
      },
      "id": "2516614c-e851-47ba-b739-827b7b517f44",
      "name": "GPT-4o",
      "type": "@n8n/n8n-nodes-langchain.lmChatOpenAi",
      "typeVersion": 1.3,
      "position": [
        -448,
        -32
      ],
      "credentials": {
        "openAiApi": {
          "id": "ICwxUBbatsF2sDvy",
          "name": "OpenAi account"
        }
      }
    }
  ],
  "pinData": {},
  "connections": {
    "Quarterly Report Trigger": {
      "main": [
        [
          {
            "node": "Download PDF",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Download PDF": {
      "main": [
        [
          {
            "node": "LlamaIndex POST",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "LlamaIndex POST": {
      "main": [
        [
          {
            "node": "Wait",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Wait": {
      "main": [
        [
          {
            "node": "Check Status",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Check Status": {
      "main": [
        [
          {
            "node": "Status Switch",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Status Switch": {
      "main": [
        [
          {
            "node": "Get Markdown",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Wait",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Get Markdown": {
      "main": [
        [
          {
            "node": "Risk Intelligence Extractor",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Risk Intelligence Extractor": {
      "main": [
        [
          {
            "node": "Split Risk Rows",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Split Risk Rows": {
      "main": [
        [
          {
            "node": "Log Risk Rows",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "GPT-4o": {
      "ai_languageModel": [
        [
          {
            "node": "Risk Intelligence Extractor",
            "type": "ai_languageModel",
            "index": 0
          }
        ]
      ]
    }
  },
  "active": false,
  "settings": {
    "executionOrder": "v1"
  },
  "versionId": "3b4b7bf4-693b-4316-8ffa-cb2d4821b73c",
  "meta": {
    "templateCredsSetupCompleted": true,
    "instanceId": "3a43da28588548e21903e71cf1dc3ddd65c24bf0c62e7e4b77542ffe87ad79c6"
  },
  "id": "iTR2t7GKdXY6On2T",
  "tags": []
}